/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverComm;

import java.io.PrintWriter;
import main.ApplicatieMain;
import model.User;
import view.MultiSpelPagina;

/**
 *
 * @author Nick
 */
public class CommOut implements Runnable {

    private final PrintWriter out;
    private final ApplicatieMain applicatieMain;
    private final CommConnection commConn;

    CommOut(PrintWriter out, CommConnection commConn) {
        applicatieMain = ApplicatieMain.getInstance();
        this.out = out;
        this.commConn = commConn;
    }

    public void run() {
        //Send username to server
        do {
            if(out!=null){
            out.println(applicatieMain.getUsername());
                System.out.println("CommOut sent :"+ applicatieMain.getUsername());
            }
        } while (out == null);
    }

    public void getLobby() {
        System.out.println("CommOut: getLobby");
        out.println("getLobby");
    }

    public void sendChatMessage(String message) {
        System.out.println("CommOut: sendChatMessage");
        synchronized (out) {
            out.println("sendChatMessage");
            out.println(message);
        }
    }

    public void setOpponent(int opponentId) {
        System.out.println("CommOut: setOpponent");
        synchronized (out) {
            out.println("setOpponent");
            out.println(opponentId);
        }
    }

    public void setMyCurrentImageAndGuessedLetters(int imageCount, String guessedLetters) {
        System.out.println("CommOut: setMyCurrentImageAndGuessedLetters");
        synchronized (out) {
            out.println("setMyCurrentImageAndGuessedLetters");
            out.println(imageCount);
            out.println(guessedLetters);
        }
    }

    public void requestGame(User opponent) {
        System.out.println("CommOut: requestGame");
        synchronized (out) {
            out.println("requestGame");
            out.println(opponent.getUserID());
        }
    }

    public void acceptGame() {
        System.out.println("CommOut: acceptGame");
        out.println("acceptGame");

        applicatieMain.setNotifyLobbyUpdate(false);
        

        MultiSpelPagina multiSpel = new MultiSpelPagina(applicatieMain.getRequestedOpponent(), true);
        applicatieMain.setMultiSpel(multiSpel);
        applicatieMain.showPage(multiSpel);
        LeaveLobby();
    }

    public void rejectGame() {
        System.out.println("CommOut: rejectGame");
        out.println("rejectGame");
    }

    public void setWord(String word) {
        System.out.println("CommOut: setWord");
        synchronized (out) {
            out.println("setWord");
            out.println(word);
        }
    }

    public void setStatus(int status) {
        System.out.println("CommOut: setCurrentStatus");
        synchronized (out) {
            out.println("currentStatusChange");
            out.println(status);
        }
    }

    public void LeaveLobby() {
        System.out.println("CommOut: leaveLobby");
        out.println("leaveLobby");
    }

    public void closeConnection() {
        System.out.println("CommOut: CLOSECONNECTION");
        out.println("CLOSECONNECTION");
    }
}
